import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
import numpy as np
import keras
# from tensorflow import keras
from keras.layers import GRU, Activation, Dense,Dropout,Conv1D,Dropout
from keras.models import Sequential
from keras.optimizers import Adam,SGD,RMSprop,Nadam
import keras.backend as K
# import tensorflow as tf

def getRecall(y_true, y_pred):
    TP = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))  # TP
    P=K.sum(K.round(K.clip(y_true, 0, 1)))
    FN = P-TP  # FN=P-TP
    recall = TP / (TP + FN + K.epsilon())  # TP/(TP+FN)
    return recall

# import DTdata
import pickle
f = open('allX.pickle', 'rb')
allX = pickle.load(f)
f = open('allY.pickle', 'rb')
allY = pickle.load(f)

model = keras.models.Sequential()
model.add(GRU(3, input_shape=(5,1)))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.8))
model.add(Dense(128, activation='tanh'))
model.add(Dropout(0.8))
model.add(Dense(32, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.8))
model.add(Dense(128, activation='tanh'))
model.add(Dropout(0.8))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer=Nadam(lr=0.005,epsilon=0.03), loss='binary_crossentropy', metrics=[getRecall, 'acc'])
lasrecall = 0
lasacc = 0
# callback1 = keras.callbacks.EarlyStopping(monitor='loss', min_delta=0.0001, patience=24, verbose=0, mode='min', baseline=0.2, restore_best_weights=True)
callback2 = keras.callbacks.EarlyStopping(monitor='getRecall', min_delta=0.00004, patience=100, verbose=0, mode='max', baseline=0.55, restore_best_weights=True)
callback3 = keras.callbacks.ReduceLROnPlateau(monitor='getRecall', factor=0.1, patience=100, verbose=0, mode='max', epsilon=0.00001, cooldown=0, min_lr=0)
checkpoint = keras.callbacks.ModelCheckpoint(filepath='model_{epoch:03d}-{getRecall:06f}.h5', monitor='getRecall', verbose=0, save_best_only=True, save_weights_only=False, mode='max', period=1)
print(model.summary())
model.fit(np.array(allX), np.array(allY), epochs=30,callbacks=[checkpoint,callback2,callback3])